<!--  -->
<template>
  <el-dialog v-model="dialogFormVisible" title="注册" :label-width="formLabelWidth">
    <el-form >
      <el-form-item label="账号">
        <el-input v-model="zcuserobj.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="zcuserobj.password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="zcuserobj.zcname" autocomplete="off" />
      </el-form-item>
      <el-form-item label="头像">
        <el-input v-model="zcuserobj.pic" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false, zcdit()">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>

  <div class="loginbox">
    <div class="loginsmbox">
      <h1>用户登录</h1>
      <div class="loinpsty">
        <el-input v-model="userobj.username" class="w-50 m-2" placeholder="账号" style="width: 100%; height: 50px;">
          <template #prefix>
            <el-icon>
              <Avatar />
            </el-icon>
          </template>
        </el-input>
        <el-input v-model="userobj.password" class="w-50 m-2" show-password placeholder="密码"
          style="width: 100%; height: 50px; margin-top: 25px;">
          <template #prefix>
            <el-icon>
              <GoodsFilled />
            </el-icon>
          </template>
        </el-input>
      </div>
      <div class="lobtn">
        <el-button type="primary" @click="logindit()">登录</el-button>
        <el-button type="success" @click="dialogFormVisible = true">注册</el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang='js'>
import { onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import { loginUser,registeUser } from '@/api/peopleuser.js'
import { peoplePiniadata } from '@/stores/peoplePinia.js'

//数据持久化处理
const piniaData = peoplePiniadata()

const dialogFormVisible = ref(false)
const formLabelWidth = '140px'
const router = useRouter()
const userobj = ref({
  username: '',
  password: ''
})
const zcuserobj = ref({
  username: '',
  password: '',
  zcname: '',
  pic: ''
})
//登录操作
const logindit = async () => {
  const res = await loginUser(userobj.value.username, userobj.value.password)
  if (res.data == false) {
    ElMessage({
      message: '用户名或密码错误',
      type: 'warning',
    })
  } else {
    ElMessage({
      message: '成功，欢迎你' + res.data.nickname,
      type: 'success',
    })
    piniaData.zcname =  res.data.nickname
    piniaData.pic =res.data.avatarUrl
    piniaData.id =res.data.id
    router.replace('/homeofone/homeshow')
  }
}

//注册操作
const zcdit = async () => {
  //判断是否正确填写
  if (zcuserobj.value.username.trim() == '' || zcuserobj.value.password.trim() == '' || zcuserobj.value.zcname.trim() == '' || zcuserobj.value.pic.trim() == '') {
    ElMessage({
      message: '请正确填写',
      type: 'warning',
    })
  } else {
     const res = await registeUser(zcuserobj.value.username.trim(), zcuserobj.value.password.trim(), zcuserobj.value.zcname.trim(), zcuserobj.value.pic.trim())
    if (res.data == false) {
      ElMessage({
        message: '用户名已被注册',
        type: 'warning',
      })
    } else {
      ElMessage({
        message: '成功',
        type: 'success',
      })
    }
  }
  //清空
  zcuserobj.value = {
    username: '',
    password: '',
    zcname: '',
    pic: ''
  }
}
</script>

<style  scoped>
.lobtn {
  width: 100%;
  height: 50px;
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.el-button {
  height: 40px;
}

.el-input {
  font-size: 20px;
}

.loinpsty {
  width: 80%;
  height: 130px;
  margin: 0 auto;
  margin-top: 30px;
}

h1 {
  text-align: center;
  margin-top: 20px;
}

.loginbox {
  width: 100vw;
  height: 100vh;
  background-color: rgb(202, 236, 225);
  overflow: hidden;
}

.loginsmbox {
  width: 600px;
  height: 400px;
  background-color: rgb(245, 235, 235);
  margin: 0 auto;
  border-radius: 20px;
  margin-top: 180px;
  box-shadow: 3px 2px 5px 2px rgba(24, 24, 24, 0.2);
  overflow: hidden;
}
</style>
